const labelService = require('../service/label.service');
const errorTypes = require('../constants/error-types');

// 验证标签是否是自己的
const verifyLabel = async (ctx, next) => {
  const { labelId } = ctx.request.body;
  const { id } = ctx.user;
  const isMyLabel = await labelService.isMyLabel(labelId, id);
  if (!isMyLabel) {
    const error = new Error(errorTypes.UN_AUTHORIZED);
    return ctx.app.emit('error', error, ctx);
  } else {
    await next();
  }
}

module.exports = {
  verifyLabel
}
